#include <widget.h>


namespace lgb_gtk
{

  void OnWidgetAnimationFinished(GooCanvasItem *item,
				 gboolean       stopped,
				 gpointer       user_data)
  {
    Widget *widget = (Widget*) user_data;
    widget->AnimationFinishedBaseFunctions();
    return ;
  }

  int Widget::AddTo(GooCanvasItem *newFather)
  {
    if( NULL == newFather )
      return 1;

    canvas = goo_canvas_group_new(newFather,"x", x , "y" , y , "can-focus", TRUE, NULL );
    Init();

    return 0;
  }

  int Widget::Alone()
  {
    if(hasInit)
      {
	goo_canvas_item_remove(canvas);
	hasInit = false;
      }
    return 0;
  }

  Widget::Widget()
  {
    hasInit = false;
    animationCompleteHandlerID = 0;
  }

  Widget::~Widget()
  {

  }

  int Widget::MoveTo(double x, double y , double time)
  {
    if(!hasInit || animating)
      return 1;

    goo_canvas_item_animate(canvas , x , y , 1 , 0 , true , time*1000 , 100 , 
			    GOO_CANVAS_ANIMATE_FREEZE);
    animationCompleteHandlerID = 
      g_signal_connect(G_OBJECT(canvas), "animation-finished",
		       G_CALLBACK(OnWidgetAnimationFinished), this);
    return 0;
  }
  /*
  int Widget::MoveTo(double x, double y , double scale , double degrees , bool absolute , double time)
  {
    if(!hasInit || animating)
      return 1;

    goo_canvas_item_animate(canvas , x , y , scale , degrees , absolute , time*1000 , 100 ,
                            GOO_CANVAS_ANIMATE_FREEZE);
    animationCompleteHandlerID =
      g_signal_connect(G_OBJECT(canvas), "animation-finished",
                       G_CALLBACK(OnWidgetAnimationFinished), this);
    return 0;
  }
  */
  int Widget::AnimationFinishedBaseFunctions()
  {
    map<int (*)(void *) , void*>::iterator it = animationFinishedFunctions.begin();
    while( it != animationFinishedFunctions.end() )
      {
        it->first(it->second);
        it++;
      }
    if(0 != animationCompleteHandlerID)
      {
	g_signal_handler_disconnect(canvas,animationCompleteHandlerID);
	animationCompleteHandlerID = 0;
      }
    return 0;
  }

  int Widget::AddMoveCompleteFunction(int (*f)(void *data), void *data)
  {
    if(NULL == f)
      return 1;

    animationFinishedFunctions[f] = data;
    return 0;
  }

}
